package com.wallet.crypto.trustapp.ui.widget.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.wallet.crypto.trustapp.R;
import com.wallet.crypto.trustapp.ui.widget.entity.SettingItem;
import com.wallet.crypto.trustapp.ui.widget.holder.SettingHolder;
import com.wallet.crypto.trustapp.ui.widget.holder.SettingPointHolder;
import com.wallet.crypto.trustapp.ui.widget.holder.VirtuaCurrencyHolder;
import com.wallet.crypto.trustapp.util.IPreference;
import com.wallet.crypto.trustapp.util.PreferenceImpl;

import java.util.ArrayList;

/**
 * Created by lingjianzhong on 2018/5/9.
 */

public class SecuritySettingsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    ArrayList<SettingItem> mItems;
    Context mContext;
    LayoutInflater mInflater;

    private static final int NORMAL_TYPE = 0;
    private static final int SWITCH_TYPE = 1;
    private static final int POINT_TYPE = 2;
    IPreference mIPreference;

    public SecuritySettingsAdapter(Context mContext, ArrayList<SettingItem> mItems) {
        this.mItems = mItems;
        this.mContext = mContext;
        this.mInflater = LayoutInflater.from(mContext);
        mIPreference = PreferenceImpl.getPreference(mContext);

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == POINT_TYPE) {
            View itemView = mInflater.inflate(R.layout.item_security_setting_rev_point, parent, false);
            return new SettingPointHolder(itemView);
        } else if (viewType == SWITCH_TYPE) {
            View itemView = mInflater.inflate(R.layout.item_togglebutton_rev, parent, false);
            return new VirtuaCurrencyHolder(itemView);
        } else {
            View itemView = mInflater.inflate(R.layout.item_setting_rev, parent, false);
            itemView.setOnClickListener(this);
            return new SettingHolder(itemView);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof VirtuaCurrencyHolder) {
            ((VirtuaCurrencyHolder) holder).mName.setText(mItems.get(position).getTitle());
            ((VirtuaCurrencyHolder) holder).mToggleButton.setChecked(mIPreference.getBoolean("Togg"));
            ((VirtuaCurrencyHolder) holder).mToggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mIPreference.put("Togg", isChecked);
                }
            });
        } else if (holder instanceof SettingHolder) {
            ((SettingHolder) holder).mIcon.setVisibility(View.GONE);
            ((SettingHolder) holder).mTitle.setText(mItems.get(position).getTitle());
            ((SettingHolder) holder).itemView.setTag(position);
        } else if (holder instanceof SettingPointHolder) {
            ((SettingPointHolder) holder).mPoint.setText(mItems.get(position).getTitle());
        }
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    @Override
    public int getItemViewType(int position) {
        int tag = mItems.get(position).getTag();
        if (tag == 2)
            return POINT_TYPE;
        else if (tag == 1)
            return SWITCH_TYPE;
        else
            return NORMAL_TYPE;
    }


    private OnItemClickListener mOnItemClickListener = null;

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null)
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

}
